public class BinarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;

    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        if(root == null){
            root = new TreeNode(key);
            return true;
        }
        TreeNode pre = null;
        TreeNode cur = root;
        while(cur != null){
            if(key < cur.key){
                pre = cur;
                cur = cur.left;
            } else if (key > cur.key) {
                pre = cur;
                cur = cur.right;
            }else{
                return false;
            }
        }
        if(key < pre.key){
            pre.left = new TreeNode(key);
        }else{
            pre.right = new TreeNode(key);
        }
        return true;
    }
    //查找key是否存在
    public boolean search(int key) {
        TreeNode cur = root;
        while(cur != null){
            if(cur.key == key) {
                return true;
            }else if(key < cur.key){
                cur = cur.left;
            }else{
                cur = cur.right;
            }
        }
        return false;
    }
    //删除key的值
    public boolean remove(int key) {
        TreeNode pre = null;
        TreeNode cur = root;
        while(cur != null){
            if(cur.key == key) {
                break;
            }else if(key < cur.key){
                pre = cur;
                cur = cur.left;
            }else{
                pre = cur;
                cur = cur.right;
            }
        }
        if(cur == null){
            return false;
        }
//        根据cur的孩子是否存在分四种情况
//        1. cur左右孩子均不存在
//        2. cur只有左孩子
//        3. cur只有右孩子
//        4. cur左右孩子均存在
//        看起来有四种情况，实际情况1可以与情况2或者3进行合并，只需要处理是那种情况即可
//        除了情况4之外，其他情况可以直接删除
//        情况4不能直接删除，需要在其子树中找一个替代节点进行删除
        if(cur.left == null && cur.right == null){
            if(pre == null){
                root = null;
                return true;
            }else if(pre.left == cur){
                pre.left = null;
            }else if(pre.right == cur){
                pre.right = null;
            }
        }else if(cur.left != null && cur.right == null){
            if(pre.left == cur){
                pre.left = cur.left;
            }else if(pre.right == cur){
                pre.right = cur.left;
            }
        }else if(cur.left == null && cur.right != null){
            if(pre.left == cur){
                pre.left = cur.right;
            }else if(pre.right == cur){
                pre.right = cur.right;
            }
        }else{
            TreeNode preTmp = cur;
            TreeNode tmp = cur.left;
            while(tmp.right != null){
                preTmp = tmp;
                tmp = tmp.right;
            }
            cur.key = tmp.key;
            if (preTmp == cur) {
                preTmp.left = tmp.left;
            } else {
                preTmp.right = tmp.left;
            }
        }
        return true;
    }
}